import { useModel } from "umi";
import { useReactive } from "ahooks";

const useLogContent = () => {
    const { remoteData } = useModel("global");
    const { selectedGateway } = useModel("gateway.info");

    const state = useReactive({
        loading: false,
        logContent: [],
    });

    const loadLogContent = async (filename) => {
        if (!selectedGateway.url) return;
        state.loading = true;
        let res = await remoteData({
            object: "multi-person-gateway",
            p2pNodeUuid: selectedGateway.uuid,
            serviceUrl: selectedGateway.url,
            serviceName: "/logs/getLogsContent",
            params: { filename },
        });
        state.logContent = res?.content || "";
        state.loading = false;
        return res?.content || "";
    };

    const setLogContent = (content) => (state.logContent = content);

    return { ...state, loadLogContent, setLogContent };
};

export default useLogContent;
